package com.boot_reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.boot_reggie.Service.impl.AdressServiceImpl;
import com.boot_reggie.Service.impl.ShoppingCartServiceImpl;
import com.boot_reggie.common.Result;
import com.boot_reggie.domain.AddressBook;
import com.boot_reggie.domain.ShoppingCart;
import com.boot_reggie.domain.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/Shopping")
public class ShoppingController {
    @Autowired
    private AdressServiceImpl adressService;
    @Autowired
    private ShoppingCartServiceImpl shoppingCartService;

    @PostMapping
    public Result<String> addCart(@RequestBody ShoppingCart shoppingCart, HttpSession session){
        User user  = (User) session.getAttribute("userInfo");
        shoppingCart.setUserId(user.getId());
        /*判断是否有相同的商品*/
        LambdaQueryWrapper<ShoppingCart> lwq = new LambdaQueryWrapper<>();
        lwq.eq(ShoppingCart::getUserId,user.getId());
        System.out.println(shoppingCart);
        /*判断是否是菜品*/
        if(shoppingCart.getDishId() != null){
            /*添加到购物车的是菜品*/
            lwq.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
            lwq.eq(ShoppingCart::getDishFlavor,shoppingCart.getDishFlavor());
        }else {
            /*添加到购物车的是套餐*/
            lwq.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        };
        /*查询套餐是否存在如果有则原先基础上加一*/
        ShoppingCart shoppingCart1 = shoppingCartService.getOne(lwq);
        if(shoppingCart1 != null){
            shoppingCart1.setNumber(shoppingCart1.getNumber() + 1);
            shoppingCartService.updateById(shoppingCart1);
        }else {
            /*否则添加*/
            shoppingCartService.save(shoppingCart);
        }

        return Result.success("数据获取成功");
    }
    @GetMapping
    public Result<List<ShoppingCart>> getAll(HttpSession session){
        User user = (User)session.getAttribute("userInfo");
        LambdaQueryWrapper<ShoppingCart> lwq = new LambdaQueryWrapper<>();
        lwq.eq(ShoppingCart::getUserId,user.getId());
        List<ShoppingCart> list = shoppingCartService.list(lwq);
        return Result.success(list,"数据获取成功");
    }
    @GetMapping("/update")
    public Result<String> dishNumber(Long id,int num,HttpSession session){
        User user = (User) session.getAttribute("userInfo");
        LambdaQueryWrapper<ShoppingCart> lwq = new LambdaQueryWrapper<>();
        lwq.eq(ShoppingCart::getUserId,user.getId());
        lwq.eq(ShoppingCart::getId,id);
        ShoppingCart shoppingCart = shoppingCartService.getOne(lwq);
//        如果是1则+
        if(num == 1){

            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartService.updateById(shoppingCart);

        }else {
            shoppingCart.setNumber(shoppingCart.getNumber() - 1);
            shoppingCartService.updateById(shoppingCart);
        }
        return Result.success("操作成功");
    }
    @DeleteMapping
    public Result<String> del(Long id){
        shoppingCartService.removeById(id);
        return Result.success("删除成功");
    }

    /*获取默认地址*/
    @GetMapping("/adress")
    public Result<AddressBook> get(HttpSession session){
        User user = (User) session.getAttribute("userInfo");
        LambdaQueryWrapper<AddressBook> lwq = new LambdaQueryWrapper<>();
        lwq.eq(AddressBook::getUserId,user.getId());
        lwq.eq(AddressBook::getIsDefault,1);
        AddressBook addressBook = adressService.getOne(lwq);
        return Result.success(addressBook,"默认地址获取成功");
    }

}
